<?php
namespace app\admin\controller;
use think\Controller;
use think\File;
class Art extends Base
{
	private $obj;
    public function _initialize(){
        $this->obj=model('art');
    }
    /**
     * 保险列表
     */
    public function index()
    {
    	$list=$this->obj->getArt();
    	// var_dump($list);exit;
        //获得类别名称 
        $cats=model('cat')->getcat(); 
        return $this->fetch('',['list'=>$list,'cats'=>$cats]);
    }
    /**
     * 保险添加
     */
    public function add(){
    	if (request()->isPost()) {
            // var_dump(input('post.'));exit;
            $validate = validate('Art');
            
    		$data['title']=input('post.title');
    		$data['number']=input('post.number');
            $data['price']=input('post.price');
            $data['content']=input('post.editorValue');
            $cat_id=input('post.cat_id');
                
    		$data['cat_id']=$cat_id;
    		$data['content']=input('post.editorValue');
            if(!$validate->check($data)){
                $this->error($validate->getError());
            }
    		$data['create_time']=time();
           
            //添加数据
			if($this->obj->add($data)){
			    	$this->success("添加成功",'art/index');
			}
    	}
        $cats=model('cat')->getCat();
    	return $this->fetch('',['cats'=>$cats]);
    }
    /**
     * 文章删除
     */
    public function del(){
    	$id=input('art_id');
    	// var_dump($id);exit;
    	if($this->obj->where('art_id',$id)->delete()){
    		$this->success("删除成功",'art/index');
    	}
    }
    public function edit(){
        $cat=model('cat')->where('parent_id','<>',0)->select();
        $id=input('art_id');
        if(request()->isPost()){
            $data['title']=input('post.title');
            $data['writer']=input('post.writer');
            $data['cat_id']=input('post.cat');
            $data['content']=input('post.editorValue');
            $data['create_time']=time();

                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('image');
                if(empty($file)){
                    $this->error('没有选择图片');
                }
                // var_dump($file);exit;
                    // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(ROOT_PATH . 'public'.'/uploads');
                if($info){
                     $data['image']='/uploads/'.$info->getSaveName();
                     // echo $data['image'];
                     
                     $image = \think\Image::open($info->getPathname());
                     $imagename=$info->getFilename();
                     // echo $imagename;
                    //将图片裁剪为300x300并保存为crop.png
                    $image->thumb(95, 105)->save(ROOT_PATH. 'public'.'/thumb/'.$imagename);
                    $data['thumb_image']='/thumb/'.$imagename;
                    // echo $data['thumb_image'];

                }
                else
                {
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
                $id=input('art_id');
                // var_dump($id);exit;
                if($this->obj->where('art_id',$id)->update($data)){
                    $this->success("修改成功",'art/index');
                }
        }
        
        // var_dump($id);exit;
        $art=$this->obj->getArtById($id);
        // var_dump($art);exit;
        return $this->fetch('',['art'=>$art,'art_id'=>$id,'cat'=>$cat]);
    }
}
